package com.atguigu.lease.web.admin.controller.apartment;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: ApartmentController
 * Package: com.atguigu.lease.web.admin.controller.apartment
 * Description:
 *
 * @Author ygf
 * @Create 2024/9/14 11:46
 * @Version 1.0
 */
@Tag(name = "公寓管理接口")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Operation(summary = "保存或更新公寓信息")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdateApartmentInfo(@RequestBody ApartmentSubmitVo apartmentSubmitVo){
        //调用service层方法
        apartmentInfoService.saveOrUpdateApartmentInfo(apartmentSubmitVo);
        return Result.ok();
    }

    @Operation(summary = "根据id删除公寓信息")
    @DeleteMapping("/removeById")
    public Result removeById(@RequestParam Long id){
        //调用service层
        apartmentInfoService.deleteApartmentInfoById(id);
        return Result.ok();
    }

    //TODO 参数匹配有问题报错状态码400
    @Operation(summary = "根据id修改公寓发布状态")//带条件的更新
    @PostMapping("/updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id, @RequestParam ReleaseStatus status){
        //创建wrapper对象
        LambdaUpdateWrapper<ApartmentInfo> wrapper = new LambdaUpdateWrapper<>();
        //设置更新的字段
        wrapper.set(ApartmentInfo::getIsRelease,status);
        //设置查询条件
        wrapper.eq(ApartmentInfo::getId,id);
        //调用service层
        apartmentInfoService.update(wrapper);
        return Result.ok();
    }

    @Operation(summary = "根据区县id查询公寓信息列表")//带条件查询
    @GetMapping("/listInfoByDistrictId")
    public Result getListInfoByDistrictId(@RequestParam Long id){
        //创建wrapper对象
        LambdaQueryWrapper<ApartmentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApartmentInfo::getDistrictId,id);
        List<ApartmentInfo> apartmentInfoList = apartmentInfoService.list(wrapper);
        return Result.ok(apartmentInfoList);
    }


    @Operation(summary = "根据条件分页查询公寓列表")
    @GetMapping("/pageItem")
    public Result<IPage<ApartmentItemVo>> pageItem(@RequestParam Long current,@RequestParam Long size,ApartmentQueryVo queryVo){
        //创建分页对象
        Page<ApartmentItemVo> page = new Page<>(current,size);
        //调用service层分页即带条件的查询
        IPage<ApartmentItemVo> iPage = apartmentInfoService.getPageList(page,queryVo);
        return Result.ok(iPage);
    }

    @Operation(summary = "根据ID获取公寓详细信息")
    @GetMapping("/getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id){
        //调用service层
        ApartmentDetailVo apartmentDetailVo = apartmentInfoService.getApartmentDetailById(id);
        return Result.ok(apartmentDetailVo);
    }



}
